package org.test.threadlocal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * Created by Hy on 2018/7/11.
 */
public class DBUtil {
    //数据库配置
    private static final String driver = "com.mysql.jdbc.Driver";
    private static final String url = "jdbc:mysql://localhost:3306/demo";
    private static final String username = "root";
    private static final String password = "admin";

    //定义一个数据库连接
//    private static Connection conn =null;

    //定义一个用于放置数据库连接的局部线程变量（使每个线程都拥有自己的连接）
    private static ThreadLocal<Connection> connContainer = new ThreadLocal<Connection>();
    //获取连接
    public static Connection getConn(){
        Connection conn = connContainer.get();
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url,username,password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

    //关闭连接
    public static void closeConn(){
        Connection conn = connContainer.get();
            try {
                if(conn!=null){
                conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

    }
}
